﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;


namespace cs_autoupdate_server
{
    public partial class Form1 : Form
    {
        SqlConnection _SqlConnection;

        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbDBType.Text)
            {
                case "mssql": tbPort.Text = "1433"; break;
                case "mysql": tbPort.Text = "3306"; break;
                default: tbPort.Text = string.Empty; break;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ConnectDB(cbDBType.Text.ToLower());
            dataGridView1.DataSource = GetTable_FileList();
        }

        private void ConnectDB(string sDBType)
        //连接数据库
        {
            //Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
            //server=服务器名(或IP);database=数据库名;uid=实际用户名;pwd=密码;";
            string ConnectString = string.Empty;
            switch (sDBType)
            {
                case "mssql":
                    ConnectString = "server=" + tbServer.Text + "," + tbPort.Text + ";database=" + tbDBName.Text + ";uid=" + tbAccount.Text + ";pwd=" + tbPassword.Text;
                    _SqlConnection = new SqlConnection(ConnectString);
                    _SqlConnection.Open();
                    gbFileManage.Enabled = true;
                    _SqlConnection.Close();
                    break;
                case "mysql":
                    ConnectString = "Server=" + tbServer.Text + ";Port=" + tbPort.Text + ";Uid=" + tbAccount.Text + ";Pwd=" + tbPassword.Text;
                    break;
            }
        }

        private DataTable GetTable_FileList()
        {
            string sql = "select ID,ProjectName,FileName,Version,MD5 from ProgramFile";
            SqlDataAdapter sda = new SqlDataAdapter(sql, _SqlConnection);
            DataTable dtFileList = new DataTable();
            _SqlConnection.Open();
            sda.Fill(dtFileList);
            _SqlConnection.Close();
            return dtFileList;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (tbProjectName.Text.Trim().Length > 0)
            {
                FileAppend();
                dataGridView1.DataSource = GetTable_FileList();
            }
            else
            {
                MessageBox.Show("“项目名称”不能为空");
                tbProjectName.Focus();
            }
        }

        private void FileAppend()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "库文件|*.dll|执行文件|*.exe|全部文件|*.*";
            ofd.Multiselect = true;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                for (int i = 0; i < ofd.FileNames.Length; i++)
                {
                    #region 删除同名已经存在的文件
                    for (int j = 0; j < dataGridView1.RowCount; j++)
                    {
                        string DBFileName = dataGridView1.Rows[j].Cells["FileName"].Value.ToString();
                        if (ofd.SafeFileNames[i].Equals(DBFileName))
                        {
                            //string id = dataGridView1.Rows[i].Cells["ID"].Value.ToString();
                            ClassFile cfDelete = new ClassFile(ofd.FileNames[i], tbProjectName.Text, _SqlConnection);
                            cfDelete.DeleteDBFile();                            
                            dataGridView1.Rows.RemoveAt(j);
                            break;
                        }
                    }
                    #endregion

                    #region 上传新的文件
                    ClassFile cfUpload = new ClassFile(ofd.FileNames[i],tbProjectName.Text,_SqlConnection);

                    cfUpload.UpLoadFile();
                    #endregion
                }
            }
        }

        private void FileOutput(string aFileName,string aProjectName)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = aFileName;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                ClassFile cfFileOutput = new ClassFile(sfd.FileName, aProjectName, _SqlConnection);
                cfFileOutput.DownloadFile();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string FileName = dataGridView1.CurrentRow.Cells["FileName"].Value.ToString();
            string ProjectName = dataGridView1.CurrentRow.Cells["ProjectName"].Value.ToString();
            FileOutput(FileName, ProjectName);
        }

    }
}
